﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL.EstRec;
using System.Data;
using System.Windows;
using System.Data.SqlClient;
namespace BAL.EstRec
{
    public class BgtVersion
    {
        private string _name;
        private long _unid;
        private DateTime _dateCreated;
        private bool _isPublished;
        private bool _isSend;
        private bool _isConsolidated;
        private BgtHqStatus _status;

#region properties
        public string name
        {
            get { return _name; }
            set { _name = value; }
        }
        public long unid
        {
            get { return _unid; }
            set { _unid = value; }
        }
        public BgtHqStatus status{
            get { return _status; }
            set { _status = value; }
        }
        public bool isPublished
        {
            get { return _isPublished; }
            set { _isPublished = value; }
        }

        public bool isSend
        {
            get { return _isSend; }
            set { _isSend = value; }
        }

        public bool isConsolidated
        {
            get { return _isConsolidated; }
            set { _isConsolidated = value; }
        }
#endregion properties

        #region Constructors
        public BgtVersion()
        {
            this.name = "";
            this.unid = 0;
            this._isPublished = false;
            this._isSend = false;
        }
        public BgtVersion(long _unid, bool _isSend, bool _isPublished)
        {
            this._unid=_unid;
            this._isPublished=_isPublished;
            this._isSend = _isSend;
        }

        #endregion

        public static BgtVersion    GetDataWithVersion(string vv)
        {

            SqlCommand sc = new SqlCommand();
            sc.CommandType = CommandType.StoredProcedure;
            sc.CommandText = "SP_SELECT_ONE_HQBGT_VERSION";
            sc.Parameters.Add(new SqlParameter("@VERSION",vv));

            DataSet res;
            res = SrvDB.ExecuteCommand(sc);

            BgtVersion gt = new BgtVersion();
            try
            {
                gt.name = res.Tables[0].Rows[0]["xname"].ToString();
                gt.unid = long.Parse(res.Tables[0].Rows[0][0].ToString());

                if (res.Tables[0].Rows[0]["SENT"].ToString() == "True")
                    gt.isSend = true;
                else
                    gt.isSend = false;

                if (res.Tables[0].Rows[0]["IS_PUBLISH"].ToString() == "True")
                    gt.isPublished = true;
                else
                    gt.isPublished = false;

                //if (  res.Tables[0].Rows[0]["IS_PUBLISH"].ToString() == "True")
                gt.isConsolidated = (res.Tables[0].Rows[0].IsNull("IS_CONSOLIDATED")) ? false : (bool)res.Tables[0].Rows[0]["IS_CONSOLIDATED"];
                //else
                    //gt.isPublished = false;
            }
            catch (Exception ex)
            {
                gt = null;
            }
            
            return gt;
        }

        public static List<BgtVersion> GetVersion()
        {
            string sqlStmt = "exec SP_SELECT_HQBGT_VERSION;";

            DataTable res;
            List<BgtVersion> list;

            try
            {
                res = SrvDB.ExecuteQuery(sqlStmt);
                list= res.AsEnumerable().Select(
                    row => new BgtVersion
                    {
                        unid = row.Field<long?>(0).GetValueOrDefault()
                        ,
                        name = String.IsNullOrEmpty(row.Field<string>(1)) ? "" : row.Field<string>(1)
                        ,
                        isPublished = row.Field<bool?>("IS_PUBLISH").GetValueOrDefault()
                        ,
                        isSend = row.Field<bool?>("SENT").GetValueOrDefault()
                    }
                ).ToList();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return list;
        }

        public static long InsertBgtVersion(BgtVersion version)
        {
            string sqlStmt = " exec SP_INSERT_HQBGT_VERSION "
                    + "@name='@@par1'"
                    + ",@date_created='2012-01-01'"
                    + ",@IS_PUBLISH='0'"
                    + ",@IS_CONSOLIDATED='0'"
                    + ",@ID_STATUS='1'"
                    + ",@WIN_USER_CREATOR='0'"
                    + ",@APP_PROFILE='0'"
                    + ",@SENT='0';";

            sqlStmt = sqlStmt.Replace("@@par1",version.name);

            long res;

            try
            {
                DataTable dt= SrvDB.ExecuteQuery(sqlStmt);
                res = (long)dt.Rows[0]["UNID_HQBGT_VERSION"];
            }catch(Exception ex)
            {
                throw ex;
            }
            
            return res;
        }

        public static void DeleteBgtVersion(long unidVersion)
        {
            int res;
            string sqlStmt = " exec dbo.SP_DELETE_HQBGT_VERSION "
                + "@UNID_HQBGT_VERSION='@@par1';";
            
            sqlStmt=sqlStmt.Replace("@@par1",unidVersion.ToString());

            try
            {
                res = SrvDB.ExecuteNonQuery(sqlStmt);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static void UpdateBgtVersion(BgtVersion version)
        {

        }
        public static bool IsPublishSharedPoint(BgtVersion isPublish)
        {
            string sqlStmt;

            DataTable dt;

            bool bandera = false;

                sqlStmt = " exec SP_ER_IS_PUBLISH_HQBGT_VERSION "
                + "@UNID_HQBGT_VERSION=@@par1"
                + ",@IS_PUBLISH=@@par2;";

            sqlStmt = sqlStmt.Replace("@@par1",isPublish._unid.ToString());
            sqlStmt = sqlStmt.Replace("@@par2", isPublish._isPublished.ToString());
            try
            {
                dt = SrvDB.ExecuteQuery(sqlStmt);
                if (dt.Rows.Count == 1)
                {
                    
                    bandera= Convert.ToBoolean( dt.Rows[0]["BANDERA"]);
                }
                else
                {
                    MessageBox.Show("Error al actualizar la publicación.", "Mensaje Sistema", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                    bandera = false;
                }

            }
            catch (Exception ex)
            {

                throw ex;
            }

            return bandera; 
        }


        public static bool IsConsolidated(BgtVersion isConsol)
        {
            string sqlStmt;

            DataTable dt;

            bool bandera = false;

            sqlStmt = " exec SP_ER_IS_CONSOLIDATED_HQBGT_VERSION "
            + "@UNID_HQBGT_VERSION=@@par1"
            + ",@IS_CONSOLIDATED=@@par2;";

            sqlStmt = sqlStmt.Replace("@@par1", isConsol._unid.ToString());
            sqlStmt = sqlStmt.Replace("@@par2", isConsol._isConsolidated.ToString());
            try
            {
                dt = SrvDB.ExecuteQuery(sqlStmt);
                if (dt.Rows.Count == 1)
                {

                    bandera = Convert.ToBoolean(dt.Rows[0]["BANDERA"]);
                }
                else
                {
                    MessageBox.Show("Error al actualizar la publicación.", "Mensaje Sistema", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                    bandera = false;
                }

            }
            catch (Exception ex)
            {

                throw ex;
            }

            return bandera;
        }


        public static bool IsSend(BgtVersion isPublish)
        {
            string sqlStmt;

            DataTable dt;

            bool bandera = false;

            sqlStmt = " exec SP_ER_IS_SEND_HQBGT_VERSION "
            + "@UNID_HQBGT_VERSION=@@par1"
            + ",@IS_SEND=@@par2;";

            sqlStmt = sqlStmt.Replace("@@par1", isPublish._unid.ToString());
            sqlStmt = sqlStmt.Replace("@@par2", isPublish._isSend.ToString());
            try
            {
                dt = SrvDB.ExecuteQuery(sqlStmt);
                if (dt.Rows.Count == 1)
                {

                    bandera = Convert.ToBoolean(dt.Rows[0]["BANDERA"]);
                }
                else
                {
                    MessageBox.Show("Error al actualizar la publicación...", "Mensaje Sistema", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                    bandera = false;
                }

            }
            catch (Exception ex)
            {

                throw ex;
            }

            return bandera;
        }
    }
}
